﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ViewMaker;
using ViewMaker.Core;

namespace CloudStorageLight.WpfClient
{
    [View(typeof(HistoryWindow))]
    public class HistoryVM : AppViewModel
    {
        public string Title { get; set; }

        public string Path { get; set; }

        public List<string> Versions { get; set; }

        public Dictionary<string, string> Items { get; set; }

        public string SelectedVersion { get; set; }

        public string FileProtectedMode { get; set; }

        public string AttachmentFileName { get; set; }

        public HistoryVM(string path, string title, List<string> versions, string fileProtectedMode, string attachmentFileName)
        {
            Path = path;
            Title = title;
            Versions = versions;
            FileProtectedMode = fileProtectedMode;
            AttachmentFileName = attachmentFileName;

            Items = new Dictionary<string, string>();
            foreach (var ver in versions)
            {
                var items = ver.Split('|');
                Items.Add(items[0], items[Math.Min(items.Length-1,1)]);

            }
        }

        private string GetFileName(string path)
        {
            var items = path.Split('/');
            return items.Last();
        }

        public ICommand ExecuteFileCommand
        {
            get
            {
                return CreateCommand((x) => {
                    if (string.IsNullOrEmpty(SelectedVersion)) return;
                    var item = new FileItem { Title = Title, Path = Path, FileProtectedMode = FileProtectedMode, AttachmentFileName = AttachmentFileName, Snapshot = SelectedVersion, IsReadOnly = true };
                    ExecuteActionAsync(ExecuteFile(item, false)); 
                });
            }
        }
        public ICommand CloseCommand
        {
            get
            {
                return CreateCommand((x) =>
                {
                    this.ExecuteViewCommand(ViewCommands.Close);
                });
            }
        }

    }
}
